package com.salary.snippet

import scala.xml.{NodeSeq,Text}
import net.liftweb.http.{FileParamHolder,S,SHtml,StatefulSnippet, RequestVar}
import net.liftweb.common.{Box,Empty,Full}
import net.liftweb.util.Helpers.{bind,chooseTemplate}
import net.liftweb.util.Helpers._
import net.liftweb.mapper._
import java.io.File
import java.io.FileOutputStream;
import java.io.FileInputStream

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.hssf.usermodel.HSSFSheet
import org.apache.poi.hssf.usermodel.HSSFRow
import org.apache.poi.hssf.usermodel.HSSFCell
import org.apache.poi.hssf.usermodel.HSSFDataFormatter
import org.apache.poi.ss.usermodel.Cell

abstract class TableComm{
	val sheetName = ""
	val firstRow = 2

	val numOfPage = 5
	var fileHolder : Box[FileParamHolder] = Empty
	
	def addItem(row: HSSFRow) = {}

	object currentSn extends RequestVar[Int](0)
	object currentSql extends RequestVar[String]("")

	def readFormula(cell: HSSFCell, isString:Boolean): String = {
		cell.getCachedFormulaResultType match {
			case Cell.CELL_TYPE_NUMERIC => {
				if (isString)
					cell.getNumericCellValue().toInt.toString
				else
					cell.getNumericCellValue().toString
			}
			case Cell.CELL_TYPE_STRING =>
				cell.getStringCellValue()

			case _ => ""
		}
	}
	def readCellBase(cell: HSSFCell, isString:Boolean) = {
		cell.getCellType match {
			case Cell.CELL_TYPE_FORMULA =>
				readFormula(cell, isString)

			case Cell.CELL_TYPE_NUMERIC => {
				if(isString)
					cell.getNumericCellValue().toInt.toString
				else
					cell.getNumericCellValue().toString
			}
			case Cell.CELL_TYPE_STRING =>
				cell.getStringCellValue()

			case _ => ""
		}
	}
	def readCell(cell: HSSFCell): String = {
		readCellBase(cell, true)
	}
	def readCellDouble(cell: HSSFCell):Double = {
		val value = readCellBase(cell, false)
		if (value == "") {
			0.0
		}
		else {
			value.toDouble
		}
	}
	def readCellInt(cell: HSSFCell):Int = {
		val value = readCell(cell)
		if (value == "") {
			0
		}
		else {
			value.toInt
		}
	}
	
	def importExcel(name:String): Boolean = {
		try{
			var workbook = new HSSFWorkbook(new FileInputStream(name))
			val sheet = workbook.getSheet(sheetName)
			var isTail:Boolean = false
			var rowSn = firstRow
			while (isTail != true) {
				val row = sheet.getRow(rowSn)
				rowSn = rowSn + 1
				if (row != null && row.getCell(0).getStringCellValue() != "") {
					addItem(row)
				}
				else
				{
					isTail = true
					println(" no items")
				}
			}
			true
		}catch {
			case e: Exception => { S.error("error info: " + e); false }
			case _ => false
		}
	}
	def uploadBase(xhtml : NodeSeq) : NodeSeq = {
		def doUpload() = {
			fileHolder match {
			// An empty upload gets reported with a null mime type,
			// so we need to handle this special case
			case Full(FileParamHolder(_, null, _, _)) => S.error("null receipt attachment")
			case Full(FileParamHolder(_, mime, name, data)) => {
				var f = new File("uploads/" + name);
				var fops = new FileOutputStream(f)
				fops.write(data)
				fops.close()
				importExcel("uploads/" + name)
				}
			case Full(_) => {
				S.error("Invalid receipt attachment")
				}
			case _ => S.error("Empty receipt attachment")
			}

		}
		
		bind("entry", xhtml,
			"filename" -> SHtml.fileUpload(fph => fileHolder = Full(fph)),
			"submit" -> SHtml.submit("Upload", doUpload) ) 
	
	}

	def getSn(sn:Int, len:Int):Int = {
		if (sn >= len) {
			if ((len%numOfPage ) == 0 ) len - numOfPage
			else len - (len%numOfPage )
		}
		else if (sn < 0 ) 0
		else sn
	}
	def GetPageMaxSn(sn:Int, len:Int) = {
		if(sn + numOfPage > len) len
		else sn + numOfPage
	}
	def saveCurrenVal(sn:Int, sql:String) = {
		currentSn(sn)
		currentSql(sql)
	}
}